package property.personal.property_mange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import property.personal.property_mange.dao.PropertyMapper;
import property.personal.property_mange.dao.StaffMapper;
import property.personal.property_mange.pojo.Propertyinfo;
import property.personal.property_mange.pojo.Staff;
import property.personal.property_mange.service.StaffService;


@Service
public class StaffImpl implements StaffService {

    @Autowired
    StaffMapper staffMapper;
    @Autowired
    PropertyMapper propertyMapper;

    @Override
    public IPage getstaff(int page, int limit,String searchname) {
        Page<Staff> p= new Page<>(page,limit);
        QueryWrapper<Staff> q=new QueryWrapper<>();
        QueryWrapper<Propertyinfo> proquery=new QueryWrapper<>();
        QueryWrapper<Staff> staffproquery=new QueryWrapper<>();
        String proid;

        if (searchname!=null && !"".equals(searchname)){
            q.like("staffname",searchname);
            IPage<Staff> ipage=staffMapper.selectPage(p,q);
            if (ipage.getRecords().size()>0){
                System.out.println("size=="+ipage.getRecords().size());
                return ipage;
            }else {
                proquery.eq("propertyname",searchname);
                Propertyinfo pro=propertyMapper.selectOne(proquery);
                proid=pro.getId();
                staffproquery.eq("proid",proid);
                ipage=staffMapper.selectPage(p,staffproquery);
                return ipage;
            }
        }else{
            IPage<Staff> ipage=staffMapper.selectPage(p,q);
            return ipage;
        }
    }

}
